﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace tree_node
{
    class bnode
    {
        public int id;
        public bnode left, right;
        public bnode()
        {
            id = 0;
            left = right = null;
        }
        public bnode(int id1)
        {
            id = id1;
            left = right = null;
        }
    }
    class binarytree
    {
        int node1,node2, node3, node4, node5, node6, node7;
        bnode root;
        bnode parent;
        bnode temp;
       public binarytree()
        { parent = temp = root = null; }

        private void nnode(int i, ref bnode r)
        {
            if (r != null)
            {

                if (i == r.id)
                {
                    parent = r;
                }
                nnode(i, ref r.left);
                nnode(i, ref r.right);
            }
        }
        public void mytree()
        {
            int a, b,c;
            if (root == null)
            {
                Console.WriteLine("What is the ID of root");
                a = Convert.ToInt32(Console.ReadLine());
                root = new bnode(a);
                node1 = a;
                Console.Write("New Node created(root):   ID: {0}",node1);
                  {
                    if (a == node1)
                    Console.Write("\nLeaf: yes   ID: {0}\n", node1); 
                    else
                    return;
                  }
         //     Split root into two child
                  int u = a + 1;
                  Console.WriteLine("\n\n\nEnter the ID of Child");
                  b = Convert.ToInt32(Console.ReadLine());
                  temp= root.left = new bnode(b);
                  Console.WriteLine("Enter the ID of Child");  
                  c = Convert.ToInt32(Console.ReadLine());  
                  temp.right = new bnode(c);  
                  temp = temp.right;
               
                   Console.Write("\nNew Node created:   ID: {0}", b);  
                   Console.Write("\nNew Node created:   ID: {0}", c);  
                   node2 = b;
                   node3 = c;
                   {
                       if (u == 0)
                           return;
                       else
                           Console.Write("\nLeaf: no    ID: {0}", node1);
                           Console.Write("\nLeaf: yes   ID: {0}", node2);
                           Console.Write("\nLeaf: yes   ID: {0}\n\n", node3);
                    }
             }
            else
            {
                mychild();
            }
        }
          public void mychild()
        {
            int d, e,f ;  
            Console.WriteLine("\n\nAt which node do you want add child?");
            d = Convert.ToInt32(Console.ReadLine());
            nnode(d, ref root);
            if (parent.left == null)
            {
            Console.WriteLine("Enter the ID of Child");  
            e = Convert.ToInt32(Console.ReadLine());
            temp = parent.left = new bnode(e);
               {
                   Console.WriteLine("Enter the ID of Child");  
                   f = Convert.ToInt32(Console.ReadLine());  
                   temp.right = new bnode(f);  
                   temp = temp.right;
                }
                Console.Write("\nNew Node created:   ID: {0}", e);  
                Console.Write("\nNew Node created:   ID: {0}", f);  
                if (d == node2)
                    node4 = e;
                if (d == node2)
                    node5 = f;
                if (d == node3)
                    node6 = e;
                if (d == node3) 
                    node7 = f;
                if (d == node3) 
                Console.Write("\nLeaf: no    ID: {0}",node1);
                if (d == node3) 
                Console.Write("\nLeaf: no    ID: {0}",node2);
                if (d == node3) 
                Console.Write("\nLeaf: yes   ID: {0}",node4);
                if (d == node3) 
                Console.Write("\nLeaf: yes   ID: {0}",node5);
                if (d == node3) 
                Console.Write("\nLeaf: no    ID: {0}",node3);
                if (d == node3) 
                Console.Write("\nLeaf: yes   ID: {0}",node6);
                if (d == node3) 
                Console.Write("\nLeaf: yes   ID: {0}",node7);
               }
            else
            {
                return;
            }
           }
    }
  } 
